package com.ww.gss.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;

import com.ww.gss.exceptions.BusinessException;
import com.ww.gss.service.UserService;
import com.ww.gss.util.DispatherConstant;
import com.ww.gss.util.GSSWebUtil;
import com.ww.gss.util.SystemConstans;

@Controller
public class UserController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService commonservice;

	@RequestMapping(value = DispatherConstant.ERROR + "/{errorcode}")
	public @ResponseBody Map error(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String errorcode) {
		return GSSWebUtil.buildResponseMap(SystemConstans.RESPONSE_LABEL_ERROR, errorcode, errorcode);
	}

	@RequestMapping(value = "hi", method = RequestMethod.GET)
	public @ResponseBody Map hi(HttpServletRequest req, HttpServletResponse res) throws BusinessException {
		String result = "hi, gss web service";
		return GSSWebUtil.buildResponseMap(SystemConstans.RESPONSE_LABEL_SUCCESS, result, "");
	}

	@RequestMapping(value = "testexception", method = RequestMethod.GET)
	public void testexception(HttpServletRequest req, HttpServletResponse res) throws BusinessException {
		String result = "hi, gss web service";
		String aa = "tt";
		int ad = Integer.valueOf(aa);

	}

	@RequestMapping(value = "user", method = RequestMethod.GET)
	public ModelAndView user(HttpServletRequest req, HttpServletResponse res) throws BusinessException {
		String result = "hi, gss service page";

		// get Info from backend
		RequestContext requestContext = new RequestContext(req);
		ModelAndView mv = new ModelAndView();
		mv.addObject("money", requestContext.getMessage("money"));
		mv.addObject("date", requestContext.getMessage("date"));

		mv.setViewName("user");
		mv.addObject("hello", result);
		mv.addObject("title", "Welcome to GSS - User");
		return mv;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletRequest req, HttpServletResponse res) throws BusinessException {
		return "index";
	}

}
